import { defineStore } from 'pinia';
import { Session } from '@/utils/storage';

import { getUserInfo } from '@/api/login';

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: () => ({
		userInfos: {
			userId: '',
			menus: [],
			nickName: '',
			authBtnList: [],
			userName: '',
			// photo: '',
			// time: 0,
			// roles: [],
		},
	}),
	actions: {
		async setUserInfos() {
			let res = await getUserInfo();
			// console.log(res);
			if (res.code === 0) {
				// 用户信息模拟数据
				const userInfos = {
					userId: res.data.id,
					menus: res.data.menus,
					nickName: res.data.nickname,
					authBtnList: res.data.permissions,
					userName: res.data.username,
				};
				// 存储用户信息到浏览器缓存
				Session.set('userInfo', userInfos);
				// if (Session.get('userInfo')) {
				// 	this.userInfos = Session.get('userInfo');
				// } else {
				this.userInfos = userInfos;
				// }
				return res.data.menus;
			}
		},
	},
});
